5f0565f2c5dfc1dbeacf93397410320de367fc67
[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
13 ;; by the Free Software Foundation; either version 3, or (at your
14 ;; option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; 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; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; For Emacs < 22.2.
31 (eval-and-compile
32   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33
34 (eval-when-compile (require 'cl))
35
36 (require 'mm-util)
37 (require 'mm-decode)
38 (require 'mm-url)
39
40 (defvar gnus-article-mime-handles)
41
42 (defvar mm-extern-function-alist
43   '((local-file . mm-extern-local-file)
44     (url . mm-extern-url)
45     (anon-ftp . mm-extern-anon-ftp)
46     (ftp . mm-extern-ftp)
47 ;;;     (tftp . mm-extern-tftp)
48     (mail-server . mm-extern-mail-server)
49 ;;;     (afs . mm-extern-afs))
50     ))
51
52 (defvar mm-extern-anonymous "anonymous")
53
54 (defun mm-extern-local-file (handle)
55   (erase-buffer)
56   (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
57         (coding-system-for-read mm-binary-coding-system))
58     (unless name
59       (error "The filename is not specified"))
60     (mm-disable-multibyte)
61     (if (file-exists-p name)
62         (mm-insert-file-contents name nil nil nil nil t)
63       (error "File %s is gone" name))))
64
65 (defun mm-extern-url (handle)
66   (erase-buffer)
67   (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
68         (name buffer-file-name)
69         (coding-system-for-read mm-binary-coding-system))
70     (unless url
71       (error "URL is not specified"))
72     (mm-with-unibyte-current-buffer
73       (mm-url-insert-file-contents url))
74     (mm-disable-multibyte)
75     (setq buffer-file-name name)))
76
77 (defun mm-extern-anon-ftp (handle)
78   (erase-buffer)
79   (let* ((params (cdr (mm-handle-type handle)))
80          (name (cdr (assq 'name params)))
81          (site (cdr (assq 'site params)))
82          (directory (cdr (assq 'directory params)))
83          (mode (cdr (assq 'mode params)))
84          (path (concat "/" (or mm-extern-anonymous
85                                (read-string (format "ID for %s: " site)))
86                        "@" site ":" directory "/" name))
87          (coding-system-for-read mm-binary-coding-system))
88     (unless name
89       (error "The filename is not specified"))
90     (mm-disable-multibyte)
91     (mm-insert-file-contents path nil nil nil nil t)))
92
93 (defun mm-extern-ftp (handle)
94   (let (mm-extern-anonymous)
95     (mm-extern-anon-ftp handle)))
96
97 (declare-function message-goto-body "message" (&optional interactivep))
98
99 (defun mm-extern-mail-server (handle)
100   (require 'message)
101   (let* ((params (cdr (mm-handle-type handle)))
102          (server (cdr (assq 'server params)))
103          (subject (or (cdr (assq 'subject params)) "none"))
104          (buf (current-buffer))
105          info)
106     (if (y-or-n-p (format "Send a request message to %s? " server))
107         (save-window-excursion
108           (message-mail server subject)
109           (message-goto-body)
110           (delete-region (point) (point-max))
111           (insert-buffer-substring buf)
112           (message "Requesting external body...")
113           (message-send-and-exit)
114           (setq info "Request is sent.")
115           (message info))
116       (setq info "Request is not sent."))
117     (goto-char (point-min))
118     (insert "[" info "]\n\n")))
119
120 ;;;###autoload
121 (defun mm-extern-cache-contents (handle)
122   "Put the external-body part of HANDLE into its cache."
123   (let* ((access-type (cdr (assq 'access-type
124                                  (cdr (mm-handle-type handle)))))
125          (func (cdr (assq (intern
126                            (downcase
127                             (or access-type
128                                 (error "Couldn't find access type"))))
129                           mm-extern-function-alist)))
130          buf handles)
131     (unless func
132       (error "Access type (%s) is not supported" access-type))
133     (mm-with-part handle
134       (goto-char (point-max))
135       (insert "\n\n")
136       ;; It should be just a single MIME handle.
137       (setq handles (mm-dissect-buffer t)))
138     (unless (bufferp (car handles))
139       (mm-destroy-parts handles)
140       (error "Multipart external body is not supported"))
141     (save-excursion
142       (set-buffer (setq buf (mm-handle-buffer handles)))
143       (let (good)
144         (unwind-protect
145             (progn
146               (funcall func handle)
147               (setq good t))
148           (unless good
149             (mm-destroy-parts handles))))
150       (mm-handle-set-cache handle handles))
151     (setq gnus-article-mime-handles
152           (mm-merge-handles gnus-article-mime-handles handles))))
153
154 ;;;###autoload
155 (defun mm-inline-external-body (handle &optional no-display)
156   "Show the external-body part of HANDLE.
157 This function replaces the buffer of HANDLE with a buffer contains
158 the entire message.
159 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
160   (unless (mm-handle-cache handle)
161     (mm-extern-cache-contents handle))
162   (unless no-display
163     (save-excursion
164       (save-restriction
165         (narrow-to-region (point) (point))
166         (mm-display-part (mm-handle-cache handle))))
167     ;; Move undisplayer added to the cached handle to the parent.
168     (mm-handle-set-undisplayer
169      handle (mm-handle-undisplayer (mm-handle-cache handle)))
170     (mm-handle-set-undisplayer (mm-handle-cache handle) nil)))
171
172 (provide 'mm-extern)
173
174 ;; arch-tag: 9653808e-14d9-4172-86e6-adceaa05378e
175 ;;; mm-extern.el ends here